<template>
  <div>
    <el-card style="margin-bottom: 20px">
      <el-breadcrumb separator-class="el-icon-arrow-right" style="margin: 20px">
        <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item to="">用户管理</el-breadcrumb-item>
        <el-breadcrumb-item to="">用户信息列表</el-breadcrumb-item>
      </el-breadcrumb>
      <el-row :gutter="20" style="margin-bottom: 20px">
        <el-col :span="8">
          <el-input placeholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="8">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加用户信息</el-button
          >
        </el-col>
      </el-row >
      <!-- 搜索框 -->
      <el-row :gutter="20" style="padding-right: 20px" >
        <el-col :span="5">
          <el-input v-model="ruleForm.username" placeholder="请输入账号"></el-input>
        </el-col>
        <el-col :span="3">
          <el-input width="200px" v-model="ruleForm.name" placeholder="请输入姓名" ></el-input>
        </el-col>
        <el-col :span="5">
            性别：
            <el-radio-group v-model="ruleForm.gender">
                <el-radio-button label="男"></el-radio-button>
                <el-radio-button label="女"></el-radio-button>
            </el-radio-group>
        </el-col>
        <el-col :span="5">
          <el-button type="primary" icon="el-icon-search" @click="getList(pageNum,pageSize)">点击搜索</el-button>
          <el-button type="" @click="onReset">清除</el-button>
        </el-col>
      </el-row>
    </el-card>
    
    <el-card style="margin-bottom: 20px">
      <el-table :data="tableData" height="450px" stripe>
        <el-table-column label="账号" prop="username"></el-table-column>
        <el-table-column label="密码" prop="password"></el-table-column>
        <el-table-column label="姓名" prop="name"></el-table-column>
        <el-table-column label="年龄" prop="age"></el-table-column>
        <el-table-column label="性别" prop="gender"></el-table-column>
        <el-table-column label="手机号码" prop="phone"></el-table-column>
        <el-table-column label="邮箱" prop="email"></el-table-column>
        <el-table-column label="照片" prop="photo">
          <template slot-scope="scope">
            <img width="100px" :src="scope.row.photo" alt="" />
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime"></el-table-column>
        <el-table-column label="操作" width="160" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="updatebefore1(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="mini"
              @click="del(scope.$index, scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <div style="margin: 20px">
        <el-pagination
          layout="prev, pager, next"
          :total="total"
          background
          :page-size="pageSize"
          @current-change="current"
        >
        </el-pagination>
        <!-- 用户添加 -->
        <el-dialog
          title="添加用户信息"
          :visible.sync="addDialogVisible"
          width="40%"
        >
          <el-form :model="addForm" :rules="rules" ref="addForm">
            <el-form-item label="账号:" prop="username">
              <el-input type="text" v-model="addForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码:" prop="password">
              <el-input type="text" v-model="addForm.password"></el-input>
            </el-form-item>
            <el-form-item label="姓名:" prop="name">
              <el-input type="text" v-model="addForm.name"></el-input>
            </el-form-item>
            <el-form-item label="年龄:" prop="age">
              <el-input type="text" v-model="addForm.age"></el-input>
            </el-form-item>
            <el-form-item label="性别:" prop="gender">
              <el-input type="text" v-model="addForm.gender"></el-input>
            </el-form-item>
            <el-form-item label="手机号码:" prop="phone">
              <el-input type="text" v-model="addForm.phone"></el-input>
            </el-form-item>
            <el-form-item label="邮箱:" prop="email">
              <el-input type="text" v-model="addForm.email"></el-input>
            </el-form-item>
            <el-form-item label="照片:">
              <el-upload
                class="upload-demo"
                action="action"
                accept="image/jpeg,image/jpg,image/png"
                :limit="1"
                :auto-upload="true"
                :http-request="uploadFileAdd"
                ref="upload"
                multiple
              >
                <el-button type="primary" size="mini">点击上传</el-button>
                <div slot="tip" class="el_upload_tip">只能上传图片</div>
              </el-upload>
            </el-form-item>
            <!-- <el-form-item label="创建时间">
              <el-input type="text" v-model="addForm.createTime"></el-input>
            </el-form-item> -->
          </el-form>
          <span slot="footer">
            <el-button @click="addDialogVisible = false">取消</el-button>
            <el-button type="primary" @click="addUser('addForm')"
              >确定</el-button
            >
          </span>
        </el-dialog>
        <!-- 用户编辑 -->
        <el-dialog
          title="编辑用户信息"
          :visible.sync="updateDialogVisible"
          width="40%"
        >
          <el-form :model="updateForm">
            <el-form-item label="id:">
              <el-input type="text" v-model="updateForm.id" disabled></el-input>
            </el-form-item>
            <el-form-item label="账号:">
              <el-input type="text" v-model="updateForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码:">
              <el-input type="text" v-model="updateForm.password"></el-input>
            </el-form-item>
            <el-form-item label="姓名:">
              <el-input type="text" v-model="updateForm.name"></el-input>
            </el-form-item>
            <el-form-item label="年龄:">
              <el-input type="text" v-model="updateForm.age"></el-input>
            </el-form-item>
            <el-form-item label="性别:">
              <el-input type="text" v-model="updateForm.gender"></el-input>
            </el-form-item>
            <el-form-item label="手机号码:">
              <el-input type="text" v-model="updateForm.phone"></el-input>
            </el-form-item>
            <el-form-item label="邮箱:">
              <el-input type="text" v-model="updateForm.email"></el-input>
            </el-form-item>
            <el-form-item label="照片:">
              <el-input type="hidden" v-model="updateForm.photo"></el-input>
              <el-upload
                class="upload-demo"
                action="action"
                accept="image/jpeg,image/jpg,image/png"
                :limit="1"
                :auto-upload="true"
                :http-request="uploadFileEdit"
                ref="upload"
                multiple
              >
                <el-button type="primary" size="mini">点击上传</el-button>
                <div slot="tip" class="el_upload_tip">只能上传图片</div>
              </el-upload>
            </el-form-item>
            <el-form-item label="创建时间">
              <el-input type="text" v-model="updateForm.createTime"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer">
            <el-button @click="updateDialogVisible = false">取消</el-button>
            <el-button type="primary" @click="updateUser">确定</el-button>
          </span>
        </el-dialog>
      </div>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      total: 3,
      imgurl: "",
      ruleForm: {
        username: null,
        name: null,
        gender: null,
      },
      tableData: [],
      pageSize: 3,
      pageNum: 1,
      addForm: {},
      updateForm: {},
      updateimgurl: "",
      addDialogVisible: false,
      updateDialogVisible: false,
      rules: {
        username: [{ required: true, message: "请输入用户名" }],
        password: [{ required: true, message: "请输入密码" }],
      },
    };
  },
  methods: {
    async getList(pageNum, pageSize) {
      // let {data:res} = await this.$axios.get('data/user.json');
      // // window.console.log(res.data.list);
      // this.tableData=res.data.list
        let { data: res } = await this.$axios.get("/user/getlist", {
        params: {
          pageNum:pageNum,
          pageSize:pageSize,
          username:this.ruleForm.username,
          name:this.ruleForm.name,
          gender:this.ruleForm.gender
        },
      });
        this.total = res.data.total;
        this.tableData = res.data.list;
     
      
    },
    current(e) {
      this.pageNum = e;
      this.getList(e, this.pageSize);
    },
    onReset(){
        window.console.log(JSON.stringify(this.ruleForm));
        this.ruleForm.username=null;
        this.ruleForm.name=null;
        this.ruleForm.gender=null;
        this.getList(this.pageNum,this.pageSize);
    },
    addUser(formName) {
      window.console.log(formName);
      window.console.log(JSON.stringify(this.addForm));
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // alert('submit!');
          this.addForm.photo = this.imgurl;
          let { data: res } = await this.$axios.post(
            "/user/add",
            JSON.stringify(this.addForm),
            {
              headers: {
                "Content-Type": "application/json",
              },
            }
          );
          window.console.log(res); //目标值为200
          if (res.code != 200) return this.$message.error(res.message);
          this.$message.success(res.code);
          this.getList(this.pageNum, this.pageSize);
          this.addDialogVisible = false;
        } else {
          alert("error submit!!");
          return false;
        }
        this.addForm={};
      });
    },
    async uploadFileAdd() {
      const formData = new FormData();
      const file = this.$refs.upload.uploadFiles[0];
      const headerConfig = {
        headers: { "Content-Type": "multipart/form-data" },
      };
      if (!file) {
        alert("请选择文件");
        return;
      }
      window.console.log("图片解析为：" + file.raw);
      formData.append("file", file.raw);
      formData.append("type", "USER");
      let { data: res } = await this.$axios.post(
        "/uploadImg",
        formData,
        headerConfig
      );
      window.console.log(res);
      window.console.log(res.data.imgurl);

      this.imgurl = res.data.imgurl;
    },
    async uploadFileEdit() {
      const formData = new FormData();
      const file = this.$refs.upload.uploadFiles[0];
      const headerConfig = {
        headers: { "Content-Type": "multipart/form-data" },
      };
      if (!file) {
        alert("请选择文件");
        return;
      }
      window.console.log("图片解析为：" + file.raw);
      formData.append("file", file.raw);
      formData.append("type", "user");
      let { data: res } = await this.$axios.post(
        "/uploadImg",
        formData,
        headerConfig
      );
      window.console.log(res);
      window.console.log(res.data.imgurl);
      this.updateimgurl = res.data.imgurl;
    },

    async updatebefore1(row) {
      let { data: res } = await this.$axios.get("/user/getlist", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          id: row.id,
        },
      });
      this.updateForm = res.data.list[0];
      this.updateDialogVisible = true;
    },
    async updateUser() {
      if (this.updateimgurl != "") {
        this.updateForm.photo = this.updateimgurl;
        this.updateimgurl = "";
        window.console.log(this.updateForm.photo);
      }
      window.console.log(JSON.stringify(this.updateForm));
      let { data: res } = await this.$axios.post(
        "/user/update",
        this.updateForm
      );
      if (res.code != 200) return this.$message.error(res.code);
      this.$message.success(res.code);

      this.updateDialogVisible = false;
      this.getList(this.pageNum, this.pageSize);
    },
    async del(index, id) {
      //   window.console.log(id);
      let { data: res } = await this.$axios.get("/user/delete", {
        params: {
          id: id,
        },
      });
      if (res.code != 200) return this.$message.error(res.code);
      this.getList(this.pageNum, this.pageSize);
    },
  },
  created() {
    this.getList(this.pageNum, this.pageSize);
  },
};
</script>
<style scoped lang="scss">
// .el-form {
//   border: 1px solid darkblue;
//   border-radius: 10px;
//   padding: 10px;
//   .el-form-item {
//     //  border: 1px solid darkblue;
//     width: 100%;
//     display: flex;
//     justify-content: space-between;
//   }
// }
</style>